package com.ruoyi.project.WebMoudle.msgTemplate.controller;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.WxConstant;
import com.ruoyi.project.WxMoudle.Bases.HttpUtil;
import com.ruoyi.project.WxMoudle.Bases.WeixinConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.WebMoudle.msgTemplate.domain.MsgTemplate;
import com.ruoyi.project.WebMoudle.msgTemplate.service.IMsgTemplateService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 短信模板Controller
 * 
 * @author ruoyi
 * @date 2020-09-08
 */
@Controller
@RequestMapping("/msgTemplate/msgTemplate")
public class MsgTemplateController extends BaseController
{
    private String prefix = "msgTemplate/msgTemplate";

    @Autowired
    private IMsgTemplateService msgTemplateService;

    @RequiresPermissions("msgTemplate:msgTemplate:view")
    @GetMapping()
    public String msgTemplate()
    {
        return prefix + "/msgTemplate";
    }

    @RequiresPermissions("msgTemplate:msgTemplate:view")
    @GetMapping("/sendMsg/{id}/{paramNum}")
    public String sendMsg(@PathVariable("id") Integer id, @PathVariable("paramNum") Integer paramNum,ModelMap mmap)
    {
        mmap.put("id",id);
        mmap.put("paramNum",paramNum);
        return prefix + "/sendMsg";
    }

    /**
     * 查询短信模板列表
     */
    @RequiresPermissions("msgTemplate:msgTemplate:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MsgTemplate msgTemplate)
    {
        startPage();
        List<MsgTemplate> list = msgTemplateService.selectMsgTemplateList(msgTemplate);
        return getDataTable(list);
    }

    /**
     * 导出短信模板列表
     */
    @RequiresPermissions("msgTemplate:msgTemplate:export")
    @Log(title = "短信模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MsgTemplate msgTemplate)
    {
        List<MsgTemplate> list = msgTemplateService.selectMsgTemplateList(msgTemplate);
        ExcelUtil<MsgTemplate> util = new ExcelUtil<MsgTemplate>(MsgTemplate.class);
        return util.exportExcel(list, "msgTemplate");
    }

    /**
     * 新增短信模板
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存短信模板
     */
    @RequiresPermissions("msgTemplate:msgTemplate:add")
    @Log(title = "短信模板", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MsgTemplate msgTemplate)
    {
        return toAjax(msgTemplateService.insertMsgTemplate(msgTemplate));
    }

    /**
     * 修改短信模板
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        MsgTemplate msgTemplate = msgTemplateService.selectMsgTemplateById(id);
        mmap.put("msgTemplate", msgTemplate);
        return prefix + "/edit";
    }

    /**
     * 修改保存短信模板
     */
    @RequiresPermissions("msgTemplate:msgTemplate:edit")
    @Log(title = "短信模板", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MsgTemplate msgTemplate)
    {
        return toAjax(msgTemplateService.updateMsgTemplate(msgTemplate));
    }

    /**
     * 删除短信模板
     */
    @RequiresPermissions("msgTemplate:msgTemplate:remove")
    @Log(title = "短信模板", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(msgTemplateService.deleteMsgTemplateByIds(ids));
    }

    @PostMapping( "/sendMsg")
    @ResponseBody
    public AjaxResult sendMsg(@RequestParam Map<String,String> map)
    {
        //1.获取模板id
        int id = Integer.parseInt(map.get("id"));
        MsgTemplate msgTemplate = msgTemplateService.selectMsgTemplateById(id);
        Integer templateId = msgTemplate.getTemplateId();
        //2.发送请求
        StringBuilder sb = new StringBuilder();
        sb.append("accountSid").append("=").append(WeixinConfig.ACCOUNT_SID);
        sb.append("&to").append("=").append(map.get("phoneNum"));
        sb.append("&param").append("=").append(map.get("params"));
        sb.append("&templateid").append("=").append(templateId);
        String body = sb.toString() + HttpUtil.createCommonParam(WeixinConfig.ACCOUNT_SID, WeixinConfig.AUTH_TOKEN);
        String result = HttpUtil.post(WeixinConfig.BASE_URL, body);
        System.out.println(result);
        return toAjax(1);
    }

}
